﻿Public Class FormClientesListado

    Dim classController As New Class_RC_Controller.rc_Clientes()

    Private m_frmCliente As FrmClientes
    Public Property frmClientes() As FrmClientes
        Get
            Return m_frmCliente
        End Get
        Set(value As FrmClientes)
            m_frmCliente = value
        End Set
    End Property

    Private Sub FormClientesListado_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        dgClientes.AutoGenerateColumns = False
        CargarCombos()
        CargarClientes()
    End Sub

    Private Sub CargarClientes()

        Dim codTipoCliente As Integer
        If cbxTipoCliente.Checked Then
            codTipoCliente = cboTipoCliente.SelectedValue
        End If

        Dim codCiudad As Integer
        If cbxCiudad.Checked Then
            codCiudad = cboCiudad.SelectedValue
        End If

        Dim dt As New DataTable
        dt = classController.get_Clientes_All(oiml.dbConn, 0, txtNombre.Text, txtRuc.Text, codTipoCliente, codCiudad)

        If Not dt Is Nothing Then
            dgClientes.DataSource = dt
        Else
            MessageBox.Show("Error: " & classController.MessageError.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

    End Sub

    Private Sub CargarCombos()
        Dim dsTipo As New DataTable
        dsTipo = classController.get_tipo_Clientes(oiml.dbConn)
        If Not dsTipo Is Nothing Then
            If dsTipo.Rows.Count > 0 Then
                cboTipoCliente.DataSource = dsTipo
                cboTipoCliente.DisplayMember = "descripcion"
                cboTipoCliente.ValueMember = "codtipocliente"
            End If
        End If

        Dim dsCiudad As New DataTable
        dsCiudad = classController.get_Ciudades(oiml.dbConn)
        If Not dsCiudad Is Nothing Then
            If dsCiudad.Rows.Count > 0 Then
                cboCiudad.DataSource = dsCiudad
                cboCiudad.DisplayMember = "descripcion"
                cboCiudad.ValueMember = "codCiudad"
            End If
        End If
    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub btnEditar_Click(sender As System.Object, e As System.EventArgs) Handles btnEditar.Click
        Try
            If dgClientes.SelectedRows.Count > 0 Then
                Dim cod As New Integer
                cod = dgClientes.SelectedRows(0).Cells(0).Value

                If frmClientes Is Nothing Then

                    frmClientes = New FrmClientes()
                    frmClientes.codCliente = cod

                    'If frmClientes.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    frmClientes.ShowDialog()
                    CargarClientes()
                    frmClientes.Dispose()
                    frmClientes = Nothing
                    'End If
                Else
                    frmClientes.codCliente = cod
                    frmClientes.ShowDialog()
                End If

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub txtBuscar_Click(sender As System.Object, e As System.EventArgs) Handles txtBuscar.Click
        CargarClientes()
    End Sub

    Private Sub txtNombre_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtNombre.KeyDown, txtRuc.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarClientes()

        End If
    End Sub

    Private Sub dgClientes_DoubleClick(sender As System.Object, e As System.EventArgs) Handles dgClientes.DoubleClick
        btnEditar_Click(sender, e)
    End Sub

    Private Sub dgClientes_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles dgClientes.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnEditar_Click(sender, e)
        End If

    End Sub

    Private Sub cbxTipoCliente_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cbxTipoCliente.KeyDown, cbxCiudad.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarClientes()
        End If
    End Sub

    Private Sub cboTipoCliente_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cboTipoCliente.KeyDown, cboCiudad.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarClientes()
        End If
    End Sub

    
End Class